﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Runtime.Serialization;
using System.Text;
using System;
using System.Threading;
using System.ComponentModel;
using System.Windows;
using System.Collections;
using System.Collections.Generic;
using BrainTechLLC.ThreadSafeObjects;
#if NO_SILVERLIGHT
using LinFu.AOP.Interfaces;
using BrainTechLLC.Logging;
using System.Collections.Specialized;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
using Mud.EmlenMudMain;
#endif
#if USE_HYPER
using Hyper.ComponentModel;
#endif

namespace BrainTechLLC
{
    [Serializable]
    [DataContract]
    public class PlayerSettingsData
    {
        [DataMember]
        public PropertyData _propData;

        public PropertyData PropData
        {
            get
            {
                if (_propData == null)
                    Interlocked.CompareExchange<PropertyData>(ref _propData, new PropertyData(), null);

                return _propData;
            }
        }
        public PlayerSettingsData() { }
        public PlayerSettingsData(params IdentifierAndValue[] initialValues)
        {
            PropData.AddProperties(initialValues);
        }

        protected string GetValid(string setting)
        {
            return string.Empty;
        }

        public string CanSet(string setting, string value, IGameThing thing)
        {
            return string.Empty;
        }

        public object GetValue(string setting, string value)
        {
            return value;
        }

        public string AddProperty(string setting, string value, IGameThing thing)
        {
            setting = GetValid(setting);

            if (string.IsNullOrEmpty(setting))
                return "Setting not found";

            string canSet = CanSet(setting, value, thing);

            if (!string.IsNullOrEmpty(canSet))
                return canSet;

            object val = GetValue(setting, value);
            PropData.AddProperty(setting, val);

            return string.Empty;
        }

        public T GetArgument<T>(string identifier)
        {
            if (_propData == null)
                return default(T);

            return PropData.GetArgument<T>(identifier);
        }

        public IdentifierAndValue GetArgumentReference(string identifier)
        {
            if (_propData == null)
                return null;

            return PropData.GetArgumentReference(identifier);
        }

        public T GetArgument<T>(string identifier, object defaultValue)
        {
            IdentifierAndValue item = GetArgumentReference(identifier);

            if (item.Value == null)
            {
                object o = Convert.ChangeType(defaultValue, typeof(T), null);
                return (T)o;
            }

            return (T)item.Value;
        }

    }
}
